﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WgFrame.Utils;
using WgFrame.Net;

public class ResponseHelper
{
    /// <summary>
    /// 游戏开始响应
    /// </summary>
    public static void HandleGameStartResponse(Dictionary<byte, object> parameters)
    {
        long playerId = -1;
        if (parameters.ContainsKey(1))
        {
            playerId = (long)parameters[1];
        }

        if (playerId == -1)
        {
            PanelManager.Open<CreatePanel>();
        }
        else
        {

            GameNetwork.Player = new Player(playerId);
            RequestHelper.SendGetPlayerInfoRequest();
        }
    }

    /// <summary>
    /// 创建玩家响应
    /// </summary>
    public static void HandleCreatePlayerResponse(Dictionary<byte, object> parameters)
    {
        long playerId = -1;
        if (parameters.ContainsKey(1))
        {
            playerId = (long)parameters[1];
        }

        GameNetwork.Player = new Player(playerId);

        RequestHelper.SendGetPlayerInfoRequest();
    }

    /// <summary>
    /// 玩家基本信息响应
    /// </summary>
    public static void HandleGetPlayerResponse(Dictionary<byte, object> parameters)
    {
        if (parameters.ContainsKey(1))
        {
            GameNetwork.Player.Name = (string)parameters[1];
        }
        if (parameters.ContainsKey(2))
        {
            GameNetwork.Player.BaseInfo.XiuWei = (long)parameters[2];
        }
        if (parameters.ContainsKey(3))
        {
            GameNetwork.Player.BaseInfo.Gold = (long)parameters[3];
        }
        if (parameters.ContainsKey(4))
        {
            GameNetwork.Player.BaseInfo.Rmb = (long)parameters[4];
        }
        if (parameters.ContainsKey(5))
        {
            Hashtable[] attributeTables = (Hashtable[])parameters[5];
            for (int i = 0; i < attributeTables.Length; i++)
            {
                GameNetwork.Player.FighAttributeMgr.Set((byte)(i + 1), new Attribute(attributeTables[i]));
            }
        }
        if (parameters.ContainsKey(6))
        {
            GameNetwork.Player.BaseInfo.ZhaoMuRemainTime = (long)parameters[6];
        }
        if (parameters.ContainsKey(7))
        {
            GameNetwork.Player.BaseInfo.ZhaoMuNeedCount = (int)parameters[7];
        }

        RequestHelper.SendGetPackInfoRequest();
    }

    /// <summary>
    /// 加载物品信息
    /// </summary>
    public static void HandleGetItemResponse(Dictionary<byte, object> parameters)
    {
        if (parameters.ContainsKey(1))
        {
            Hashtable[] tabals = (Hashtable[])parameters[1];
            for (int i = 0; i < tabals.Length; i++)
            {
                Item item = new Item(tabals[i]);
                GameNetwork.Player.PackItemMgr.Add(item);
            }
        }

        if (parameters.ContainsKey(2))
        {
            Hashtable[] tabals = (Hashtable[])parameters[2];
            for (int i = 0; i < tabals.Length; i++)
            {
                Equip equip = new Equip(tabals[i]);
                if (equip.szPos > 0)
                {
                    GameNetwork.Player.FightEquipMgr.Add(equip);
                }
                else
                {
                    GameNetwork.Player.PackEquipMgr.Add(equip);
                }
            }
        }

        if (parameters.ContainsKey(3))
        {
            Hashtable[] tabals = (Hashtable[])parameters[3];
            for (int i = 0; i < tabals.Length; i++)
            {
                Hero item = new Hero(tabals[i]);
                if (item.szPos > 0)
                {
                    GameNetwork.Player.FightHeroMgr.Add(item);
                }
                else
                {
                    GameNetwork.Player.PackHeroMgr.Add(item);
                }
            }
        }

        PanelManager.Open<MainPanel>();
        //GameNetwork.Player.PlayFight();
    }

    /// <summary>
    /// 穿装备响应
    /// </summary>
    public static void HandleWearEquipResponse(Dictionary<byte, object> parameters)
    {
        byte szPos = (byte)parameters[1];
        long equipId = (long)parameters[2];
        Hashtable szAttribute = (Hashtable)parameters[3];

        if (!GameNetwork.Player.PackEquipMgr.TryGet(equipId, out Equip wearEquip))
        {
            return;
        }

        if (GameNetwork.Player.FightEquipMgr.TryGetByType(szPos, wearEquip.InitEquip.kind, out Equip oldEquip))
        {
            oldEquip.szPos = 0;
            GameNetwork.Player.FightEquipMgr.Remove(szPos, oldEquip.id);
            GameNetwork.Player.PackEquipMgr.Add(oldEquip);
        }

        wearEquip.szPos = szPos;
        GameNetwork.Player.PackEquipMgr.Remove(wearEquip.id);
        GameNetwork.Player.FightEquipMgr.Add(wearEquip);
        GameNetwork.Player.FighAttributeMgr.Set(szPos, new Attribute(szAttribute));

        MsgListenerMgr.FireMsg(MsgListenerType.EquipPosChange, szPos);
    }

    /// <summary>
    /// 脱装备响应
    /// </summary>
    public static void HandleDownEquipResponse(Dictionary<byte, object> parameters)
    {
        byte szPos = (byte)parameters[1];
        long equipId = (long)parameters[2];
        Hashtable szAttribute = (Hashtable)parameters[3];

        if (!GameNetwork.Player.FightEquipMgr.TryGet(szPos, equipId, out Equip fightEquip))
        {
            return;
        }

        fightEquip.szPos = 0;
        GameNetwork.Player.FightEquipMgr.Remove(szPos, equipId);
        GameNetwork.Player.PackEquipMgr.Add(fightEquip);
        GameNetwork.Player.FighAttributeMgr.Set(szPos, new Attribute(szAttribute));

        MsgListenerMgr.FireMsg(MsgListenerType.EquipPosChange, szPos);
    }

    /// <summary>
    /// 上阵响应
    /// </summary>
    public static void HandleShangZhenResponse(Dictionary<byte, object> parameters)
    {
        long id = (long)parameters[1];
        byte pos = (byte)parameters[2];
        Hashtable szAttribute = (Hashtable)parameters[3];
        byte bzPos = (byte)parameters[4];

        if (!GameNetwork.Player.PackHeroMgr.TryGet(id, out Hero hero))
        {
            return;
        }

        GameNetwork.Player.PackHeroMgr.Remove(id);
        bool isAdd = true;
        if (GameNetwork.Player.FightHeroMgr.TryGetByPos(pos, out Hero fightHero))
        {
            GameNetwork.Player.FightHeroMgr.RemoveByPos(pos);
            fightHero.szPos = 0;
            GameNetwork.Player.PackHeroMgr.Add(fightHero);
            isAdd = false;
        }

        hero.szPos = pos;
        hero.bzPos = bzPos;
        GameNetwork.Player.FightHeroMgr.Add(hero);
        GameNetwork.Player.FighAttributeMgr.Set(pos, new Attribute(szAttribute));


        MsgListenerMgr.FireMsg(MsgListenerType.ShangZhen, id, isAdd);
    }

    /// <summary>
    /// 布阵响应
    /// </summary>
    public static void HandleBuZhenResponse(Dictionary<byte, object> parameters)
    {
        Hashtable[] baseInfos = (Hashtable[])parameters[1];
        for (int i = 0; i < baseInfos.Length; i++)
        {
            Hashtable info = baseInfos[i];
            long id = (long)info[(byte)1];

            if (!GameNetwork.Player.FightHeroMgr.TryGetById(id, out Hero hero))
            {
                continue;
            }

            hero.bzPos = (byte)info[(byte)2];
        }
        MsgListenerMgr.FireMsg(MsgListenerType.BuZhen);
    }

    /// <summary>
    /// 聊天响应
    /// </summary>
    public static void HandleChatResponse(Dictionary<byte, object> parameters)
    {
        PopupPanelManager.Get<SocialPanel>();
        MsgListenerMgr.FireMsg(MsgListenerType.Chat, parameters);
        MsgListenerMgr.FireMsg(MsgListenerType.NewSocialMsg);
    }

    /// <summary>
    /// 强化装备请求
    /// </summary>
    public static void HandleQiangHuaEquipResponse(Dictionary<byte, object> parameters)
    {
        long equipId = (long)parameters[1];
        short qhLv = (short)parameters[2];
        long gold = (long)parameters[3];

        if (!GameNetwork.Player.FightEquipMgr.TryGet(equipId, out Equip wearEquip))
        {
            return;
        }
        wearEquip.QiangHuaLevel = qhLv;
        wearEquip.CountAttribute();
        GameNetwork.Player.BaseInfo.Gold = gold;

        MsgListenerMgr.FireMsg(MsgListenerType.EquipAttributeChange);
        MsgListenerMgr.FireMsg(MsgListenerType.BaseInfoChange);
    }

    /// <summary>
    /// 精炼装备请求
    /// </summary>
    public static void HandleJingLianEquipResponse(Dictionary<byte, object> parameters)
    {
        long equipId = (long)parameters[1];
        short jlLv = (short)parameters[2];
        int count = (int)parameters[3];
        long gold = (long)parameters[4];
        int itemId = (int)parameters[5];

        if (!GameNetwork.Player.FightEquipMgr.TryGet(equipId, out Equip wearEquip))
        {
            return;
        }

        if (GameNetwork.Player.PackItemMgr.TryGetBySId(itemId, out Item item))
        {
            item.Count = count;
        }

        wearEquip.JingLianLevel = jlLv;
        wearEquip.CountAttribute();
        GameNetwork.Player.BaseInfo.Gold = gold;

        MsgListenerMgr.FireMsg(MsgListenerType.EquipAttributeChange);
        MsgListenerMgr.FireMsg(MsgListenerType.BaseInfoChange);
    }

    /// <summary>
    /// 装备注灵请求
    /// </summary>
    public static void HandleZhuLinEquipResponse(Dictionary<byte, object> parameters)
    {
        long equipId = (long)parameters[1];
        short jlLv = (short)parameters[2];
        int count = (int)parameters[3];
        int itemId = (int)parameters[4];

        if (!GameNetwork.Player.FightEquipMgr.TryGet(equipId, out Equip wearEquip))
        {
            return;
        }

        if (GameNetwork.Player.PackItemMgr.TryGetBySId(itemId, out Item item))
        {
            item.Count = count;
        }

        wearEquip.ZhuLingLevel = jlLv;
        wearEquip.CountAttribute();

        MsgListenerMgr.FireMsg(MsgListenerType.EquipAttributeChange);
    }

    /// <summary>
    /// 装备灵石强化请求
    /// </summary>
    public static void HandleLinShiQiangHuaResponse(Dictionary<byte, object> parameters)
    {
        long equipId = (long)parameters[1];
        short[] jlLv = (short[])parameters[2];
        int count = (int)parameters[3];
        int itemId = (int)parameters[4];

        if (!GameNetwork.Player.FightEquipMgr.TryGet(equipId, out Equip wearEquip))
        {
            return;
        }

        if (jlLv == null)
        {
            return;
        }

        if (GameNetwork.Player.PackItemMgr.TryGetBySId(itemId, out Item item))
        {
            item.Count = count;
        }

        int len = jlLv.Length > wearEquip.LingShiLevel.Length ? wearEquip.LingShiLevel.Length : jlLv.Length;
        for (int i=0; i<len; i++)
        {
            wearEquip.LingShiLevel[i] = jlLv[i];
        }
        wearEquip.CountAttribute();

        MsgListenerMgr.FireMsg(MsgListenerType.EquipAttributeChange);
    }


    /// <summary>
    /// 购买商品响应
    /// </summary>
    public static void HandleBuyShopResponse(Dictionary<byte, object> parameters)
    {
        long gold = (long)parameters[1];
        long rmb = (long)parameters[2];
        byte itemType = (byte)parameters[3];
        Hashtable itemInfo = (Hashtable)parameters[4];
        if (itemType == 1)
        {
            //消耗品
            Item buyItem = new Item(itemInfo);
            int oldCount = 0;
            if (GameNetwork.Player.PackItemMgr.TryGet(buyItem.Id, out Item item))
            {
                oldCount = item.Count;
                item.Count = buyItem.Count;
            }
            else
            {
                GameNetwork.Player.PackItemMgr.Add(buyItem);
            }

            if (buyItem.TryGetStaticInfo(out InitItem initItem1))
            {
                FlyTextPanelMgr.Show(string.Format("{0}×{1}", initItem1.name, buyItem.Count - oldCount));
            }
        }
        else if (itemType == 2)
        {
            //装备
            Equip buyEquip = new Equip(itemInfo);
            GameNetwork.Player.PackEquipMgr.Add(buyEquip);
            string name = buyEquip.InitEquip != null ? buyEquip.InitEquip.name : "null";
            FlyTextPanelMgr.Show(string.Format("{0}×1", name));
        }
        else if (itemType == 3)
        {
            //英雄
            Hero buyHero = new Hero(itemInfo);
            GameNetwork.Player.PackHeroMgr.Add(buyHero);
            string name = buyHero.InitHero != null ? buyHero.InitHero.name : "null";
            FlyTextPanelMgr.Show(string.Format("{0}×1", name));
        }


        GameNetwork.Player.BaseInfo.Gold = gold;
        GameNetwork.Player.BaseInfo.Rmb = rmb;
        MsgListenerMgr.FireMsg(MsgListenerType.BaseInfoChange);
        MsgListenerMgr.FireMsg(MsgListenerType.BuyOk);
    }

    /// <summary>
    /// 招募响应
    /// </summary>
    public static void HandleZhaoMuResponse(Dictionary<byte, object> parameters)
    {
        Hashtable[] tabals = (Hashtable[])parameters[1];
        long rmb = (long)parameters[2];
        int time = (int)parameters[3];
        int count = (int)parameters[4];
        List<int> addHeros = new List<int>();
        for (int i=0; i< tabals.Length; i++)
        {
            Hero hero = new Hero(tabals[i]);
            GameNetwork.Player.PackHeroMgr.Add(hero);
            addHeros.Add(hero.sId);
        }

        GameNetwork.Player.BaseInfo.Rmb = rmb;
        if (time > 0)
        {
            GameNetwork.Player.BaseInfo.ZhaoMuRemainTime = time;
        }
        GameNetwork.Player.BaseInfo.ZhaoMuNeedCount = count;
        MsgListenerMgr.FireMsg(MsgListenerType.BaseInfoChange);
        MsgListenerMgr.FireMsg(MsgListenerType.ZhaoMuOk, addHeros);
    }
}
